package com.sx.manage.controller.fund;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.service.fund.IMRebateRuleSetService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.manage.mybatis.form.fund.MRebateRuleSetForm;
import com.sx.model.manage.mybatis.vo.fund.MRebateRuleSetVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;

@Controller
@RequestMapping("/mRebateRules")
public class MRebateRuleSetController extends MBaseController{

	@Resource(name="rebateRuleSetServiceImpl")
	private IMRebateRuleSetService rebateRuleSetServiceImpl;
	

	/**
	 * 添加页面 进入
	 * @return
	 */
	 @RequestMapping("/toAddView")
	public ViewAndModel toAddView() {
		ViewAndModel model=new ViewAndModel(request,cache);
		
		model.setViewName("fund/addChannelSetting");//修改ftl页面名称
		model.addObject("rebateRulesVO",new MRebateRuleSetVO());
		return model;
	}
	
	/**
	 * 保存规则信息
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/saveRule")
	@ResponseBody
	public ResultBooleanVO saveRule(MRebateRuleSetForm form){
	
		if(null != form.getId()){
			return this.rebateRuleSetServiceImpl.edit( form , loginConsumer());
		}else{
			return this.rebateRuleSetServiceImpl.add( form, loginConsumer());
		}
	}
	
	/**
	 * 进入编辑界面
	 * @param vo
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	 @RequestMapping("/toEditView/{id}")
	 @ResponseBody
	public ViewAndModel  toEditView(MRebateRuleSetForm form){

		 ViewAndModel model=new ViewAndModel(request,cache);
		 //查询类
		 ResultObjectVO<MRebateRuleSetVO>  result = this.rebateRuleSetServiceImpl.queryById( form);
		 if (null != result.getObjVo()) {
			 model.addObject("rebateRulesVO", result.getObjVo());
		}
		 ////界面路径
		 model.setViewName("fund/addChannelSetting");
		 return model;
	}
	
	/**
	 * [RebateRule]删除 流程
	 * 
	 * @return
	 *@throws ServletException
	 *@throws IOException
	 */
	 @RequestMapping("/delRules")
	public ResultBooleanVO delRules(MRebateRuleSetForm form){
		if(null == form){
			return new ResultBooleanVO(false,null,"删除错误");
		}
		return this.rebateRuleSetServiceImpl.delelete(form, loginConsumer());
	}
	 
	 /**
	  * 列表界面
	  * @return
	  */
	 @RequestMapping("/ruleIndex")
	public ViewAndModel ruleIndex() {
			ViewAndModel model=new ViewAndModel(request,cache);
			//界面路径
			model.setViewName("fund/channelIncomeSetting");
			return model;
	}
	/**
	 * 分页数据查询
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
    @RequestMapping("/rulesPage")
    @ResponseBody		 
	public ResultListVO<MRebateRuleSetVO> rulesPage(MRebateRuleSetForm form){
    	
		return this.rebateRuleSetServiceImpl.queryPage(form);
	}
	
	//===========================如有新增加方法 请在下面增加 便于维护================================
    /**
     * 启用
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/enableRule")
    @ResponseBody
    public ResultBooleanVO enableRule(MRebateRuleSetForm form){
    	form.setData_status( MDataStatusType.DATA_AVAILABLE );
    	return rebateRuleSetServiceImpl.editStatus(form, loginConsumer());
    }
    
    /**
     * 停用
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/disableRule")
    @ResponseBody
    public ResultBooleanVO disableRule(MRebateRuleSetForm form){
    	form.setData_status( MDataStatusType.DATA_DISABLE );
    	return rebateRuleSetServiceImpl.editStatus(form, loginConsumer());
    }

	
}



